# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     welfare
   Description :
   Author :       Flyoung
   date：          2023/9/5
-------------------------------------------------
   Change Activity:
                   2023/9/5:
-------------------------------------------------
"""
from typing import AnyStr

from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

from db.common import with_session

Base = declarative_base()


class Welfare(Base):
    __tablename__ = 'welfare'

    welfare_id = Column(Integer, primary_key=True, autoincrement=True)
    welfare_name = Column(String(255), nullable=False)
    welfare_score = Column(Float, nullable=True)

    def __init__(self, welfare_name, welfare_score):
        self.welfare_name = welfare_name
        self.welfare_score = welfare_score

    def to_dict(self):
        return {
            'welfare_id': self.welfare_id,
            'welfare_name': self.welfare_name,
            'welfare_score': self.welfare_score
        }


@with_session
def get_all_welfare(session: Session):
    return [i.to_dict() for i in session.query(Welfare).all()]


@with_session
def add_all_welfare(session: Session, welfare_names: list[str], welfare_scores=None):
    if welfare_scores is None:
        welfare_scores = [0] * len(welfare_names)
    welfare_list = list()
    for i, wn in enumerate(welfare_names):
        welfare_list.append(Welfare(wn, welfare_scores[i]))
    session.add_all(welfare_list)
